import { queryPlanList,  actionPlanAdd, actionPlanEdit, actionPlanExport,actionPlanStop,actionPlanDel,
  queryPlanInfo,queryDevForPlan
} from '../services/plan';


export default {
  namespace: 'plan',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
      curing_submit:false,
      plan_curing:false,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_plan_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryPlanList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 批量删除中止
    *action_plan_stop({ payload }, { call, put }) {
      const response = yield call(actionPlanStop, payload);
    },

    // 批量删除操作
    *action_plan_del({ payload }, { call, put }) {
      const response = yield call(actionPlanDel, payload);
    },

    // 导出
    *action_plan_export({ payload }, { call, put }) {
      const response = yield call(actionPlanExport, payload);
    },


    // 路线养护点保存
    // *action_plan_curing({ payload }, { call, put }) {
    //   yield put({
    //     type: 'changeLoading',
    //     payload: { curing_submit :true },
    //   });
    //   const response = yield call(actionPathCuringAdd, payload);
    //   yield put({
    //     type: 'changeLoading',
    //     payload: { curing_submit :false },
    //   });
    // },



    // 新增计划信息
    *add_plan({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionPlanAdd,payload);
      response && (
        yield put({
          type: 'path/fetch_curing_clear',
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 修改计划
    *edit_plan({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionPlanEdit,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },


    // 获取计划详情
    *fetch_plan_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryPlanInfo, payload);
      if(response && response.data.planId){
          // 获取详情塞入右侧设备点
          const devList = yield call(queryDevForPlan, { planId: response.data.planId});
          devList && devList.data && devList.data.list && (yield put({
            type: 'path/appendPathDev',
            payload: devList.data.list,
          }));
      }
      yield put({
        type: 'appendDataDetail',
        payload:response.data ,
      });
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },


  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },


    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        loading:{}
      };
    },
  },

};
